package org.openmobo.playn.poc1.core;

import static playn.core.PlayN.*;

import java.util.ArrayList;
import java.util.logging.Logger;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;

import playn.core.Canvas;
import playn.core.CanvasImage;
import playn.core.Game;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
//import playn.core.Layer;
import playn.core.Pointer;

import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;

enum Estados { INICIAR, ATIV1, ATIV2, ATIV3, ENCERRAR };

public class Poc1ConcordanciaNominal implements Game {
	private World world;
	private float pWidth, pHeight;
	private int width, height;
	private float physUnitPerScreenUnit;
	private GroupLayer slideLayer;
	private GroupLayer ballLayer;
	Logger log = Logger.getLogger("Poc1");
	ArrayList<Ball> bolas = new ArrayList<Ball>();
	ArrayList<Text> textos = new ArrayList<Text>();
	Image ballImage;
	private Body ground;
	Estados slideAtual = Estados.INICIAR; // state represents in which screen the activity is running
	private CanvasImage bgImage;

	@Override
	public void init() {
		// create and add background image layer
		// Image bgImage = assetManager().getImage("images/bg.png");
		// width = bgImage.width();
		// height = bgImage.height();
		width = 640;
		height = 480;
		physUnitPerScreenUnit = 1 / 26.666667f;
		pWidth = width * physUnitPerScreenUnit;
		pHeight = height * physUnitPerScreenUnit;
		// ImageLayer bgLayer = graphics().createImageLayer(bgImage);
		// bgLayer.setSize(width, height);
		// graphics().rootLayer().add(bgLayer);
		Vec2 gravity = new Vec2(0.0f, 10.0f);
		world = new World(gravity, true);
		// world.setWarmStarting(true);
		// world.setAutoClearForces(true);

		bgImage = graphics().createImage(width, height);
		Canvas canvas = bgImage.canvas();
		canvas.setFillColor(0xff87ceeb);
		canvas.fillRect(0, 0, width, height);
		ImageLayer bgLayer = graphics().createImageLayer(bgImage);
		graphics().rootLayer().add(bgLayer);

		ballImage = assetManager().getImage("images/ball.png");
		ballLayer = graphics().createGroupLayer();
		ballLayer.setScale(1f / physUnitPerScreenUnit);
		graphics().rootLayer().add(ballLayer);
		slideLayer = graphics().createGroupLayer();
		graphics().rootLayer().add(slideLayer);

		Text title1 = new Text(world, "Título do Slide", 32f, false, width * 0.5f, height * 0.1f);
		textos.add(title1);
		final Text iniciar = new Text(world, "Iniciar", 28f, false, width * 0.5f, height * 0.6f);
		textos.add(iniciar);
		slideLayer.add(title1.layer);
		slideLayer.add(iniciar.layer);
		
		pointer().setListener(new Pointer.Adapter() {
			@Override
			public void onPointerEnd(Pointer.Event event) {
				switch (slideAtual) {
					case INICIAR:
						Ball ball = new Ball(world, ballImage, physUnitPerScreenUnit
								* event.x(), physUnitPerScreenUnit * event.y());
						bolas.add(ball);
						ballLayer.add(ball.layer);
						log.info("balls=" + bolas.size());
						Text text = new Text(world, "teste",12f, true, event.x(), event.y());
						textos.add(text);
						log.info("text=" + textos.size());
						slideLayer.add(text.layer);
						log.info("text added");
						if (iniciar.isClicked(event.x(), event.y())) {
							log.info("Iniciar clicked");
							clearBalls();
							clearTexts();
							Canvas canvas = bgImage.canvas();
							canvas.setFillColor(0xff87ceeb);
							canvas.fillRect(0, 0, width, height);
							slideAtual = Estados.ATIV1;
						}
						break;
					case ATIV1:
						break;
				}
			}
		});
		
	    ground = world.createBody(new BodyDef());
	    PolygonShape bottom = new PolygonShape();
	    bottom.setAsEdge(new Vec2(0, pHeight * 0.8f), new Vec2(pWidth, pHeight * 0.8f));
	    ground.createFixture(bottom, 0.0f);
	    
		CanvasImage ground = graphics().createImage(width, height);
		Canvas groundCanvas = bgImage.canvas();
		groundCanvas.setFillColor(0xff000000);
		groundCanvas.drawLine(0, height * 0.8f, width, height * 0.8f);
		ImageLayer groundLayer = graphics().createImageLayer(ground);
		graphics().rootLayer().add(groundLayer);
	    
	}

	@Override
	public void paint(float alpha) {
		switch (slideAtual) {
			case INICIAR:
				for (Ball bola : bolas) {
					bola.paint(alpha);
				}
				for (Text texto : textos) {
					texto.paint(alpha);
				}
				break;
			case ATIV1:
				break;
		}
	}

	public void clearBalls() {
		for (Ball ball : bolas) {
			ballLayer.remove(ball.layer);
			world.destroyBody(ball.body);
		}
		bolas.clear();
	}
	
	public void updateBalls(float delta) {
		ArrayList<Ball> removeBalls = new ArrayList<Ball>();
		for (Ball ball : bolas) {
			if (ball.layer.transform().translation().y() >= (height - ballImage
					.height()) * physUnitPerScreenUnit) {
				ballLayer.remove(ball.layer);
				world.destroyBody(ball.body);
				removeBalls.add(ball);
			}
		}
		for (Ball ball : removeBalls) {
			bolas.remove(ball);
		}
		removeBalls.clear();		
	}
	
	public void clearTexts() {
		for (Text texto : textos) {
			slideLayer.remove(texto.layer);
			if (texto.animated) {
				world.destroyBody(texto.body);
			}
		}
		textos.clear();
	}

	public void updateTexts(float delta) {
		ArrayList<Text> removeTexts = new ArrayList<Text>();
		for (Text texto : textos) {
			if (texto.layer.transform().translation().y() >= (height - texto.height())) {
				slideLayer.remove(texto.layer);
				if (texto.animated) {
					world.destroyBody(texto.body);
				}
				removeTexts.add(texto);
			}
		}
		for (Text texto : removeTexts) {
			textos.remove(texto);
		}
		removeTexts.clear();		
	}

	@Override
	public void update(float delta) {
		world.step(0.033f, 10, 10);
		updateBalls(delta);
		updateTexts(delta);
	}

	@Override
	public int updateRate() {
		return 25;
	}

}
